package com.example.annotationdemo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author: yeyangtao
 * @date: 2020/4/7 14:02
 */
@Component
@Aspect
public class HelloAspect {
    @Pointcut("@annotation(com.example.annotationdemo.annotation.CheckParam)")
    public void check() {
    }

    @Before("check()")
    public void deal(JoinPoint joinPoint) throws NoSuchFieldException, IllegalAccessException {
        Object[] objects = joinPoint.getArgs();
        for (Object o : objects) {
            Class c = o.getClass();
            Field field = c.getDeclaredField("name");
            field.setAccessible(true);
            String result = field.get(o).toString();
            System.out.println(result);
        }
    }
}
